package logic

import (
	"context"
	"fmt"
	"project/model/mysql"
	"project/pkg"
	"project/projectrpc/internal/svc"
	"project/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *projectrpc.UserRegisterRequest) (*projectrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line

	password1 := pkg.Md5(in.Password)

	if password1 == in.Password {
		return nil, fmt.Errorf("密码加密失败，请重新试一试")
	}

	user := mysql.User{
		Tel:      in.Tel,
		Password: password1,
	}

	err := mysql.DB.Create(&user).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}

	return &projectrpc.UserRegisterResponse{Id: int64(user.ID)}, nil
}
